package com.geek.javaes.game;

abstract  class Role {
        private String name;   // 角色名称
        private int hp;        // 生命值
        private int attack;
        private int defense;// 攻击力

    public Role() {
    }

    public Role(String name, int hp, int attack,int defense ) {
        this.name = name;
        this.hp = hp;
        this.attack = attack;
        this.defense =defense ;
    }


    // 【抽象方法】强制子类实现“攻击逻辑”（多态核心）
        public abstract void attack(Role target);

        // 【通用行为】承受伤害（所有角色复用）
        public void takeDamage(int damage) {
            hp -= damage;
            if (hp <= 0) {
                hp = 0;
                System.out.println(name + " 被击败了！");
            } else {
                System.out.println(name + " 剩余生命值：" + hp);
            }
        }

        // Getter/Setter：供子类/外部访问属性
        public String getName() { return name; }
        public int getHp() { return hp; }
        public int getAttack() { return attack; }
        public void setAttack(int attack) { this.attack = attack; }
    public int getDefense() { return defense; } // 新增防御 Getter
    public void setDefense(int defense) { this.defense = defense; } // 新增防御 Setter

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 设置
     * @param hp
     */
    public void setHp(int hp) {
        this.hp = hp;
    }

    public String toString() {
        return "Role{name = " + name + ", hp = " + hp + ", attack = " + attack + "}";
    }
}

